package com.hw.paltfrom.service.casemain;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hw.paltfrom.mapper.HwBaseAnnexesfileMapper;
import com.hw.paltfrom.mapper.HwBaseCountycoreMapper;
import com.hw.paltfrom.mapper.HwBaseEnterpriseMapper;
import com.hw.paltfrom.mapper.HwBaseOffsiteMapper;
import com.hw.paltfrom.mapper.HwBaseOverrunstationMapper;
import com.hw.paltfrom.mapper.HwDataEnterpriseMapper;
import com.hw.paltfrom.mapper.HwDataOffsiteMapper;
import com.hw.paltfrom.mapper.HwDataOverrunstationMapper;
import com.hw.paltfrom.pack.ResponseResult;
import com.hw.paltfrom.pojo.HwBaseAnnexesfile;
import com.hw.paltfrom.pojo.HwBaseCountycore;
import com.hw.paltfrom.pojo.HwBaseEnterprise;
import com.hw.paltfrom.pojo.HwBaseOffsite;
import com.hw.paltfrom.pojo.HwBaseOverrunstation;
import com.hw.paltfrom.pojo.HwDataEnterprise;
import com.hw.paltfrom.pojo.HwDataOffsite;
import com.hw.paltfrom.pojo.HwDataOverrunstation;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.utils.DateTimeUtil;

@Service
public class StationInfoDetailServer {
	@Autowired
	private HwBaseOverrunstationMapper hbosMapper;
	@Autowired
	private HwBaseEnterpriseMapper hbepMapper;
	@Autowired
	private HwBaseOffsiteMapper baseoffsiteMapper;

	@Autowired
	private HwDataOverrunstationMapper hdosMapper;
	@Autowired
	private HwDataEnterpriseMapper hdepMapper;
	@Autowired
	private HwDataOffsiteMapper dataoffsiteMapper;
	
	@Autowired
	private HwBaseAnnexesfileMapper hbafMapper;
	@Autowired
	private HwBaseCountycoreMapper countycoreMapper;

	// 查站基础信息
	public Map<String, Object> queryStationInfo(String stationCode) {

		HashMap<String, Object> result = new HashMap<String, Object>();

		// 基础信息
		HwBaseOverrunstation entity = new HwBaseOverrunstation();
		entity.setfOverruncode(stationCode);
		HwBaseOverrunstation stationInfo = hbosMapper.selectOne(entity);
		result.put("simple", stationInfo);

		// 图片，固定返回一张
		String getfPic = stationInfo.getfPic();
		Wrapper<HwBaseAnnexesfile> fileWrapper = new EntityWrapper<HwBaseAnnexesfile>();
		fileWrapper.eq("F_FolderId", getfPic);
		List<HwBaseAnnexesfile> picList = hbafMapper.selectList(fileWrapper);
		if (!picList.isEmpty()) {
			String filePath = picList.get(0).getFilePath();
			result.put("picPath", filePath);
		}
		Wrapper<HwBaseCountycore> corewrapper=new EntityWrapper<HwBaseCountycore>();
		corewrapper.eq("F_AreaId", stationInfo.getfAreaid());
		List<HwBaseCountycore> selectList = countycoreMapper.selectList(corewrapper);
		if(!selectList.isEmpty()){
			String getfCountycorename = selectList.get(0).getfCountycorename();
			result.put("jg", getfCountycorename);
		}
		return ResponseResult.successNotice(HWSystemCode.SUCCESS, result);
	}

	// 查源头企业基础信息
	public Map<String, Object> queryEnterpriseInfo(String enterpriseCode) {

		HashMap<String, Object> result = new HashMap<String, Object>();

		// 基础信息
		HwBaseEnterprise entity = new HwBaseEnterprise();
		entity.setfEnterprisecode(enterpriseCode);
		HwBaseEnterprise enterpriseInfo = hbepMapper.selectOne(entity);
		result.put("simple", enterpriseInfo);

		// 图片，固定返回一张
		String getfPic = enterpriseInfo.getfPic();
		Wrapper<HwBaseAnnexesfile> fileWrapper = new EntityWrapper<HwBaseAnnexesfile>();
		fileWrapper.eq("F_FolderId", getfPic);
		List<HwBaseAnnexesfile> picList = hbafMapper.selectList(fileWrapper);
		if (!picList.isEmpty()) {
			String filePath = picList.get(0).getFilePath();
			result.put("picPath", filePath);
		}

		return ResponseResult.successNotice(HWSystemCode.SUCCESS, result);
	}
	
//	查看非现场检测点信息
	public Map<String, Object> queryOffsiteInfo(String offsiteCode) {
		
		HashMap<String, Object> result = new HashMap<String, Object>();
		
		// 基础信息
		HwBaseOffsite entity=new HwBaseOffsite();
		entity.setfOffsitecode(offsiteCode);
		HwBaseOffsite offsiteInfo = baseoffsiteMapper.selectOne(entity);
		result.put("simple", offsiteInfo);

		// 图片，固定返回一张
		String getfPic = offsiteInfo.getfPic();
		Wrapper<HwBaseAnnexesfile> fileWrapper = new EntityWrapper<HwBaseAnnexesfile>();
		fileWrapper.eq("F_FolderId", getfPic);
		List<HwBaseAnnexesfile> picList = hbafMapper.selectList(fileWrapper);
		if (!picList.isEmpty()) {
			String filePath = picList.get(0).getFilePath();
			result.put("picPath", filePath);
		}
		return ResponseResult.successNotice(HWSystemCode.SUCCESS, result);
	}
	

//	选中的超限站 最新的六条数据
	public Map<String, Object> queryStationData(String stationCode) {
		
		PageHelper.startPage(1, 6);
		HashMap<String, Long> afterTimeRound = DateTimeUtil.getAfterTimeRound(0);
		Timestamp startTime =new Timestamp(afterTimeRound.get("startTime")) ;
		Timestamp endTime =new Timestamp(afterTimeRound.get("endTime")) ;
		Wrapper<HwDataOverrunstation> wrapper=new EntityWrapper<HwDataOverrunstation>();
		wrapper.eq("F_OverrunCode", stationCode);
//		wrapper.between("D_JCSJ", startTime, endTime);
		wrapper.orderBy("D_JCSJ",false);
		List<HwDataOverrunstation> selectList = hdosMapper.selectList(wrapper);
		PageInfo<HwDataOverrunstation> info = new PageInfo<HwDataOverrunstation>(selectList);

		return ResponseResult.successNotice(HWSystemCode.SUCCESS, info);
	}
	
//	选中的源头企业 最新的六条数据
	public Map<String, Object> queryEnterPriseData(String enterpriseCode) {
		PageHelper.startPage(1, 6);
		HashMap<String, Long> afterTimeRound = DateTimeUtil.getAfterTimeRound(0);
		Timestamp startTime =new Timestamp(afterTimeRound.get("startTime")) ;
		Timestamp endTime =new Timestamp(afterTimeRound.get("endTime")) ;
		Wrapper<HwDataEnterprise> wrapper=new EntityWrapper<HwDataEnterprise>();
		wrapper.eq("F_EnterpriseCode", enterpriseCode);
//		wrapper.between("JCSJ", startTime, endTime);
		wrapper.orderBy("JCSJ",false);
		List<HwDataEnterprise> selectList = hdepMapper.selectList(wrapper);
		PageInfo<HwDataEnterprise> info = new PageInfo<HwDataEnterprise>(selectList);
		return ResponseResult.successNotice(HWSystemCode.SUCCESS, info);
	}
//	选中的非现场监测点最新的六条数据
	public Map<String, Object> queryOffsiteData(String offsiteCode) {
		PageHelper.startPage(1, 6);
		HashMap<String, Long> afterTimeRound = DateTimeUtil.getAfterTimeRound(0);
		Timestamp startTime =new Timestamp(afterTimeRound.get("startTime"));
		Timestamp endTime =new Timestamp(afterTimeRound.get("endTime"));
		Wrapper<HwDataOffsite> wrapper=new EntityWrapper<HwDataOffsite>();
		wrapper.eq("F_OffSiteCode", offsiteCode);
//		wrapper.between("JCSJ", startTime, endTime);
		wrapper.orderBy("JCSJ",false);
		List<HwDataOffsite> selectList = dataoffsiteMapper.selectList(wrapper);
		PageInfo<HwDataOffsite> info = new PageInfo<HwDataOffsite>(selectList);
		return ResponseResult.successNotice(HWSystemCode.SUCCESS, info);
		
	}
}
